package controller;

import command.BinderCommand;
import entity.Address;
import entity.Point;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.Errors;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.support.RequestContext;

import javax.servlet.http.HttpServletRequest;
import java.beans.PropertyEditorSupport;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/binder")
public class BinderController {

    @InitBinder
    public void init(WebDataBinder dataBinder){
        System.out.println("BinderController.init()");

        PropertyEditorSupport pointEditor=new PropertyEditorSupport(){

            @Override
            public void setAsText(String text) throws IllegalArgumentException {
                if(text==null||"".equals(text.trim())){
                    return;
                }

                text=text.substring(1,text.length()-1);

                String[] s=text.split(",");

                Point point=new Point();
                point.setX(Double.parseDouble(s[0]));
                point.setY(Double.parseDouble(s[1]));

                setValue(point);

            }

            @Override
            public String getAsText() {
                Point point= (Point) getValue();
                return "("+point.getX()+","+point.getY()+")";
            }
        };




        dataBinder.registerCustomEditor(Point.class,pointEditor);


        // dataBinder.registerCustomEditor(Date.class,new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"),true));






    }


    @RequestMapping("/show")
    public String show(BinderCommand binderCommand){
        System.out.println("BinderController.show()");

        binderCommand.setUsername("abc");

        Point point=new Point();
        point.setX(5.0);
        point.setY(7.0);
        binderCommand.setPoint(point);


        Point p=new Point();
        p.setX(9.0);
        p.setY(4.0);
        binderCommand.setP(p);

        binderCommand.setBirthday(new Date());

        Address address=new Address();
        address.setProvince("江苏省");
        address.setCity("南京市");
        binderCommand.setAddress(address);

        return "binder";
    }

    @RequestMapping("/submit")
    public String submit(BinderCommand binderCommand, Errors errors, Map map, HttpServletRequest request){
        System.out.println("BinderController.submit()");

        RequestContext requestContext=new RequestContext(request);




        if(errors.hasErrors()){
            Map<String,String> errorMap=new HashMap<String,String>();

            List<FieldError> fieldErrors=errors.getFieldErrors();

            for (FieldError fieldError : fieldErrors) {
                System.out.println("fieldError = " + fieldError);

                System.out.println("fieldError.getRejectedValue() = " + fieldError.getRejectedValue());
                System.out.println("fieldError.getCode() = " + fieldError.getCode());
                System.out.println("fieldError.getObjectName() = " + fieldError.getObjectName());
                System.out.println("fieldError.getField() = " + fieldError.getField());
                System.out.println("fieldError.getDefaultMessage() = " + fieldError.getDefaultMessage());


                String message=requestContext.getMessage(fieldError.getCode()+"."+fieldError.getObjectName()+"."+fieldError.getField());


                // errorMap.put(fieldError.getField(), fieldError.getDefaultMessage());
                errorMap.put(fieldError.getField(), message);
            }

            map.put("errors",errorMap);

            return "binder";
        }

        System.out.println(binderCommand);

        return "redirect:/binder/show";
    }


}
